{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "读取数据，并分开成训练与测试2部分数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>play_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4e11f45d732f4861772b2906f81a7d384552ad12</td>\n",
       "      <td>SOCKSGZ12A58A7CA4B</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4e11f45d732f4861772b2906f81a7d384552ad12</td>\n",
       "      <td>SOCVTLJ12A6310F0FD</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4e11f45d732f4861772b2906f81a7d384552ad12</td>\n",
       "      <td>SODLLYS12A8C13A96B</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4e11f45d732f4861772b2906f81a7d384552ad12</td>\n",
       "      <td>SOEGIYH12A6D4FC0E3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4e11f45d732f4861772b2906f81a7d384552ad12</td>\n",
       "      <td>SOFRQTD12A81C233C0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       user                song  play_count\n",
       "0  4e11f45d732f4861772b2906f81a7d384552ad12  SOCKSGZ12A58A7CA4B           1\n",
       "1  4e11f45d732f4861772b2906f81a7d384552ad12  SOCVTLJ12A6310F0FD           1\n",
       "2  4e11f45d732f4861772b2906f81a7d384552ad12  SODLLYS12A8C13A96B           3\n",
       "3  4e11f45d732f4861772b2906f81a7d384552ad12  SOEGIYH12A6D4FC0E3           1\n",
       "4  4e11f45d732f4861772b2906f81a7d384552ad12  SOFRQTD12A81C233C0           2"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train = pd.read_csv(\"triplet_dataset_sub(2).csv\")\n",
    "df_train.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有播放就用1表示，没有播放用0表示，数据源都是有播放的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>play_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>37519.000000</td>\n",
       "      <td>37519.000000</td>\n",
       "      <td>37519.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>388.839948</td>\n",
       "      <td>331.289480</td>\n",
       "      <td>8.799222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>231.144277</td>\n",
       "      <td>236.602529</td>\n",
       "      <td>33.401146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>185.000000</td>\n",
       "      <td>116.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>394.000000</td>\n",
       "      <td>306.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>584.000000</td>\n",
       "      <td>532.000000</td>\n",
       "      <td>8.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>789.000000</td>\n",
       "      <td>799.000000</td>\n",
       "      <td>3532.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               user          song    play_count\n",
       "count  37519.000000  37519.000000  37519.000000\n",
       "mean     388.839948    331.289480      8.799222\n",
       "std      231.144277    236.602529     33.401146\n",
       "min        0.000000      0.000000      1.000000\n",
       "25%      185.000000    116.000000      1.000000\n",
       "50%      394.000000    306.000000      3.000000\n",
       "75%      584.000000    532.000000      8.000000\n",
       "max      789.000000    799.000000   3532.000000"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#df_train[\"play_count\"] = 1    \n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "labelencoder=LabelEncoder()\n",
    "for col in (\"user\",\"song\"):\n",
    "    df_train[col] = labelencoder.fit_transform(df_train[col])\n",
    "df_train.head()\n",
    "df_train.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/chsl/.local/lib/python3.6/site-packages/sklearn/model_selection/_split.py:2069: FutureWarning: From version 0.21, test_size will always complement train_size unless both are specified.\n",
      "  FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "train_part,test_part = train_test_split(df_train,train_size = 0.6,random_state = 1)\n",
    "\n",
    "train_data = train_part.values\n",
    "test_data = test_part.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "781\n"
     ]
    }
   ],
   "source": [
    "from RS_User_CF import User_base_CF\n",
    "\n",
    "music_User_based_CF = User_base_CF(train_data,790)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "800\n"
     ]
    }
   ],
   "source": [
    "from RS_Item_CF import Item_base_CF\n",
    "\n",
    "music_Item_based_CF = Item_base_CF(train_data,800)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the 0-th step is running\n",
      "the rmse of this step on train data is 28.5102491606104\n",
      "the 1-th step is running\n",
      "the rmse of this step on train data is 28.545333771714755\n",
      "the 2-th step is running\n",
      "the rmse of this step on train data is 28.560779712733652\n",
      "the 3-th step is running\n",
      "the rmse of this step on train data is 28.539693484964353\n",
      "the 4-th step is running\n",
      "the rmse of this step on train data is 28.56428220278266\n",
      "the 5-th step is running\n",
      "the rmse of this step on train data is 28.56526506242701\n",
      "the 6-th step is running\n",
      "the rmse of this step on train data is 28.566546461556396\n",
      "the 7-th step is running\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/chsl/ai/homework_week5/RS_SVD_CF.py:50: RuntimeWarning: overflow encountered in multiply\n",
      "  ans = self.mu + self.bi[i_id] + self.bu[uid] + np.sum(self.qi[i_id]*self.pu[uid])\n",
      "/home/chsl/.local/lib/python3.6/site-packages/numpy/core/fromnumeric.py:83: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the rmse of this step on train data is 28.56457457595742\n",
      "the 8-th step is running\n",
      "the rmse of this step on train data is 28.515544689264033\n",
      "the 9-th step is running\n",
      "the rmse of this step on train data is 28.508976068848185\n",
      "the 10-th step is running\n",
      "the rmse of this step on train data is 28.538703518334472\n",
      "the 11-th step is running\n",
      "the rmse of this step on train data is 28.519569870123025\n",
      "the 12-th step is running\n",
      "the rmse of this step on train data is 28.50243087159683\n",
      "the 13-th step is running\n",
      "the rmse of this step on train data is 28.513450872108297\n",
      "the 14-th step is running\n",
      "the rmse of this step on train data is 28.505317177146622\n",
      "the 15-th step is running\n",
      "the rmse of this step on train data is 28.535415835588502\n",
      "the 16-th step is running\n",
      "the rmse of this step on train data is 28.521451416161423\n",
      "the 17-th step is running\n",
      "the rmse of this step on train data is 28.461130300567362\n",
      "the 18-th step is running\n",
      "the rmse of this step on train data is 28.49269756585955\n",
      "the 19-th step is running\n",
      "the rmse of this step on train data is 28.472696754715468\n",
      "the 20-th step is running\n",
      "the rmse of this step on train data is 28.472272380733465\n",
      "the 21-th step is running\n",
      "the rmse of this step on train data is 28.471436095843334\n",
      "the 22-th step is running\n",
      "the rmse of this step on train data is 28.461124057283126\n",
      "the 23-th step is running\n",
      "the rmse of this step on train data is 28.442419272507742\n",
      "the 24-th step is running\n",
      "the rmse of this step on train data is 28.412753685396783\n"
     ]
    }
   ],
   "source": [
    "from RS_SVD_CF import SVD_CF\n",
    "\n",
    "music_SVD_CF = SVD_CF(train_data)\n",
    "music_SVD_CF.train()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试数据看看"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the rmse on test data is 40.615905575455116\n"
     ]
    }
   ],
   "source": [
    "rate_pre_SVD = music_SVD_CF.test(test_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>playcount</th>\n",
       "      <th>score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>64.0</td>\n",
       "      <td>428.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>76.0</td>\n",
       "      <td>363.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>680.0</td>\n",
       "      <td>262.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>520.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>459.0</td>\n",
       "      <td>234.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>772.0</td>\n",
       "      <td>669.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>770.0</td>\n",
       "      <td>298.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>496.0</td>\n",
       "      <td>438.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>279.0</td>\n",
       "      <td>510.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>246.0</td>\n",
       "      <td>334.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>683.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>480.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>556.0</td>\n",
       "      <td>768.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>569.0</td>\n",
       "      <td>767.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>14.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>69.0</td>\n",
       "      <td>532.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>772.0</td>\n",
       "      <td>152.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>394.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>17.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>157.0</td>\n",
       "      <td>487.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>18.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>478.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>614.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     user   song  playcount  score  rank\n",
       "0    64.0  428.0        1.0    5.0   1.0\n",
       "1    76.0  363.0        1.0    5.0   2.0\n",
       "3   680.0  262.0       13.0    5.0   3.0\n",
       "4   520.0   23.0        3.0    5.0   4.0\n",
       "8   459.0  234.0        1.0    5.0   5.0\n",
       "10  772.0  669.0        6.0    5.0   6.0\n",
       "12  770.0  298.0       50.0    5.0   7.0\n",
       "14  496.0  438.0        1.0    5.0   8.0\n",
       "15  279.0  510.0        7.0    5.0   9.0\n",
       "17  246.0  334.0        2.0    5.0  10.0\n",
       "25  683.0   68.0        5.0    5.0  11.0\n",
       "26  480.0    7.0        1.0    5.0  12.0\n",
       "28  556.0  768.0        8.0    5.0  13.0\n",
       "29  569.0  767.0        5.0    5.0  14.0\n",
       "30   69.0  532.0        1.0    5.0  15.0\n",
       "33  772.0  152.0        7.0    5.0  16.0\n",
       "34  394.0   38.0        1.0    5.0  17.0\n",
       "35  157.0  487.0       24.0    5.0  18.0\n",
       "36  478.0    4.0        7.0    5.0  19.0\n",
       "37  614.0   52.0        3.0    5.0  20.0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rate_pre_SVD"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算svd得到的分数召回率  ？是指播放2次以上？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.65"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recall = 0\n",
    "rate_val = rate_pre_SVD.values\n",
    "for i in range(rate_val.shape[0]):\n",
    "    if (rate_val[i][2] > 1):\n",
    "        recall += 1\n",
    "\n",
    "recallRate = recall / 20\n",
    "\n",
    "recallRate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the test data size is 15008.000000\n",
      "the rmse on test data is 40.025742\n"
     ]
    }
   ],
   "source": [
    "rate_pre_User = music_User_based_CF.test(test_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>playcount</th>\n",
       "      <th>score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8311</th>\n",
       "      <td>180.0</td>\n",
       "      <td>373.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>149.862043</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12669</th>\n",
       "      <td>492.0</td>\n",
       "      <td>373.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>116.130001</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7162</th>\n",
       "      <td>187.0</td>\n",
       "      <td>212.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>100.673136</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13100</th>\n",
       "      <td>730.0</td>\n",
       "      <td>411.0</td>\n",
       "      <td>69.0</td>\n",
       "      <td>87.674849</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14609</th>\n",
       "      <td>34.0</td>\n",
       "      <td>373.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>86.863669</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7350</th>\n",
       "      <td>250.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>74.486165</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10149</th>\n",
       "      <td>495.0</td>\n",
       "      <td>735.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>72.607360</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6510</th>\n",
       "      <td>295.0</td>\n",
       "      <td>373.0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>71.774040</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13568</th>\n",
       "      <td>166.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>68.642343</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1925</th>\n",
       "      <td>109.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>66.673421</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14575</th>\n",
       "      <td>149.0</td>\n",
       "      <td>117.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>66.169128</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>889</th>\n",
       "      <td>470.0</td>\n",
       "      <td>689.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>58.322034</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9169</th>\n",
       "      <td>713.0</td>\n",
       "      <td>377.0</td>\n",
       "      <td>70.0</td>\n",
       "      <td>55.471299</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6700</th>\n",
       "      <td>682.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>104.0</td>\n",
       "      <td>54.960078</td>\n",
       "      <td>14.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11417</th>\n",
       "      <td>4.0</td>\n",
       "      <td>177.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>52.440199</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1054</th>\n",
       "      <td>4.0</td>\n",
       "      <td>248.0</td>\n",
       "      <td>105.0</td>\n",
       "      <td>52.117166</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1410</th>\n",
       "      <td>172.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>51.368408</td>\n",
       "      <td>17.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6598</th>\n",
       "      <td>263.0</td>\n",
       "      <td>377.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>50.089409</td>\n",
       "      <td>18.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6652</th>\n",
       "      <td>55.0</td>\n",
       "      <td>727.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>49.075287</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11509</th>\n",
       "      <td>127.0</td>\n",
       "      <td>304.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>48.191466</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        user   song  playcount       score  rank\n",
       "8311   180.0  373.0       11.0  149.862043   1.0\n",
       "12669  492.0  373.0        2.0  116.130001   2.0\n",
       "7162   187.0  212.0        2.0  100.673136   3.0\n",
       "13100  730.0  411.0       69.0   87.674849   4.0\n",
       "14609   34.0  373.0        2.0   86.863669   5.0\n",
       "7350   250.0   50.0        2.0   74.486165   6.0\n",
       "10149  495.0  735.0        1.0   72.607360   7.0\n",
       "6510   295.0  373.0       34.0   71.774040   8.0\n",
       "13568  166.0   50.0       24.0   68.642343   9.0\n",
       "1925   109.0   50.0       11.0   66.673421  10.0\n",
       "14575  149.0  117.0        1.0   66.169128  11.0\n",
       "889    470.0  689.0       10.0   58.322034  12.0\n",
       "9169   713.0  377.0       70.0   55.471299  13.0\n",
       "6700   682.0    2.0      104.0   54.960078  14.0\n",
       "11417    4.0  177.0        9.0   52.440199  15.0\n",
       "1054     4.0  248.0      105.0   52.117166  16.0\n",
       "1410   172.0    2.0        1.0   51.368408  17.0\n",
       "6598   263.0  377.0       18.0   50.089409  18.0\n",
       "6652    55.0  727.0        4.0   49.075287  19.0\n",
       "11509  127.0  304.0        1.0   48.191466  20.0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rate_pre_User"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recall = 0\n",
    "rate_val = rate_pre_User.values\n",
    "for i in range(rate_val.shape[0]):\n",
    "    if (rate_val[i][2] > 1):\n",
    "        recall += 1\n",
    "\n",
    "recallRate = recall / 20\n",
    "\n",
    "recallRate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the test data size is 15008.000000\n",
      "the rmse on test data is 40.263771\n"
     ]
    }
   ],
   "source": [
    "rate_pre_Item = music_Item_based_CF.test(test_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>playcount</th>\n",
       "      <th>score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>10516</th>\n",
       "      <td>604.0</td>\n",
       "      <td>735.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>500.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12167</th>\n",
       "      <td>604.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>683.0</td>\n",
       "      <td>337.179054</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5261</th>\n",
       "      <td>326.0</td>\n",
       "      <td>415.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>323.376472</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2566</th>\n",
       "      <td>323.0</td>\n",
       "      <td>142.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>306.852902</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2551</th>\n",
       "      <td>296.0</td>\n",
       "      <td>514.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>304.789474</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8736</th>\n",
       "      <td>296.0</td>\n",
       "      <td>86.0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>293.569977</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5218</th>\n",
       "      <td>498.0</td>\n",
       "      <td>248.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>288.000000</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11109</th>\n",
       "      <td>498.0</td>\n",
       "      <td>727.0</td>\n",
       "      <td>160.0</td>\n",
       "      <td>288.000000</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11560</th>\n",
       "      <td>326.0</td>\n",
       "      <td>658.0</td>\n",
       "      <td>295.0</td>\n",
       "      <td>258.963964</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14921</th>\n",
       "      <td>326.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>249.642862</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3675</th>\n",
       "      <td>326.0</td>\n",
       "      <td>507.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>246.258199</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10212</th>\n",
       "      <td>604.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>243.697699</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12666</th>\n",
       "      <td>326.0</td>\n",
       "      <td>335.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>217.559921</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3587</th>\n",
       "      <td>326.0</td>\n",
       "      <td>751.0</td>\n",
       "      <td>97.0</td>\n",
       "      <td>202.368041</td>\n",
       "      <td>14.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058</th>\n",
       "      <td>408.0</td>\n",
       "      <td>700.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>201.184513</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14726</th>\n",
       "      <td>326.0</td>\n",
       "      <td>159.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>176.539160</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7727</th>\n",
       "      <td>581.0</td>\n",
       "      <td>328.0</td>\n",
       "      <td>118.0</td>\n",
       "      <td>168.000000</td>\n",
       "      <td>17.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4416</th>\n",
       "      <td>66.0</td>\n",
       "      <td>213.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>145.000000</td>\n",
       "      <td>18.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14744</th>\n",
       "      <td>66.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>145.000000</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13971</th>\n",
       "      <td>326.0</td>\n",
       "      <td>324.0</td>\n",
       "      <td>325.0</td>\n",
       "      <td>140.514753</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        user   song  playcount       score  rank\n",
       "10516  604.0  735.0        1.0  500.000000   1.0\n",
       "12167  604.0    2.0      683.0  337.179054   2.0\n",
       "5261   326.0  415.0        3.0  323.376472   3.0\n",
       "2566   323.0  142.0        3.0  306.852902   4.0\n",
       "2551   296.0  514.0       82.0  304.789474   5.0\n",
       "8736   296.0   86.0       85.0  293.569977   6.0\n",
       "5218   498.0  248.0       21.0  288.000000   7.0\n",
       "11109  498.0  727.0      160.0  288.000000   8.0\n",
       "11560  326.0  658.0      295.0  258.963964   9.0\n",
       "14921  326.0    0.0        1.0  249.642862  10.0\n",
       "3675   326.0  507.0        4.0  246.258199  11.0\n",
       "10212  604.0   68.0        4.0  243.697699  12.0\n",
       "12666  326.0  335.0        9.0  217.559921  13.0\n",
       "3587   326.0  751.0       97.0  202.368041  14.0\n",
       "14058  408.0  700.0        9.0  201.184513  15.0\n",
       "14726  326.0  159.0       53.0  176.539160  16.0\n",
       "7727   581.0  328.0      118.0  168.000000  17.0\n",
       "4416    66.0  213.0        1.0  145.000000  18.0\n",
       "14744   66.0   26.0        3.0  145.000000  19.0\n",
       "13971  326.0  324.0      325.0  140.514753  20.0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rate_pre_Item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.85"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recall = 0\n",
    "rate_val = rate_pre_Item.values\n",
    "for i in range(rate_val.shape[0]):\n",
    "    if (rate_val[i][2] > 1):\n",
    "        recall += 1\n",
    "\n",
    "recallRate = recall / 20\n",
    "\n",
    "recallRate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "基于用户协同过滤，点击2次以上的为0.8，基于物品协同过滤，点击2次以上的为0.85，基于模型的为0.65"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
